#include "client.h"
#include "tcp_client.h"
#include <sys/socket.h> // 套接字基础
#include <netinet/in.h> // IPv4/IPv6
#include <arpa/inet.h>  // 地址转换
#include <netdb.h>      // 主机名解析
#include <sys/types.h>
#include <stdlib.h>

int connect_to_server(const char *ip, const char *port){
    int sock_fd = socket(AF_INET,SOCK_STREAM,0);
    ERROR_CHECK(sock_fd, -1, "socket");

    //int reuse = 1;
    //int sret = setsockopt(sock_fd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse));
    //ERROR_CHECK(sret,-1,"setsockopt");

    struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(atoi(port));
    inet_aton(ip, &addr.sin_addr);
    int cret = connect(sock_fd,(struct sockaddr *)&addr,sizeof(addr));
    ERROR_CHECK(cret, -1, "connect");

    //listen(sock_fd,100);
    return sock_fd;
}
